import { defineConfig, loadEnv } from "vite";
import path from "path";
import { createVitePlugins } from "./build/vite/plugins";
import defineENV from "./build/vite/define";
import { convertEnv } from "./build/vite/utils";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const isBuild = command === "build";
  const env = loadEnv(mode, process.cwd());
  const viteEnv: ViteEnv = convertEnv(env);
  const config = {
    base: viteEnv.VITE_PUBLIC_PATH,
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
        "#": path.resolve(__dirname, "types")
      }
    },
    server: {
      host: "0.0.0.0"
      //https:true,
      // proxy:{
      //   '^/yfs': {
      //     target: 'http://127.0.0.1:8000',
      //     changeOrigin: true
      //   }
      // }
    },
    build: {
      minify: false,
      assetsDir: "",
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
      // rollupOptions: {
      //   external: Object.keys(pkg.dependencies)
      // }
    },
    define: {
      VITE_ENV: JSON.stringify(viteEnv),
      ...defineENV
    },
    plugins: createVitePlugins(viteEnv, isBuild)
  };
  return config;
});
